// /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/  电话格式
// /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/ 邮箱地址
// /^[\u4E00-\u9FA5]?$/ 中文

export function checkPhone(value,nullMsg='手机号不能为空',errorMsg='请输入正确的手机号'){
    var reg = /^1(3|4|5|6|7|8|9)\d{9}$/; // add by fjw in 2021.10.13: 增加 16 和 19 号段
    return new Promise((resolve,reject)=>{
      if(isEmpty(value) && !isEmpty(nullMsg)){
        reject(nullMsg)
       }else if(!isEmpty(value) && !(reg.test(value))){
        reject(errorMsg)
      }else{
        resolve(true)
      }
    })
  }
  export function checkPhoneRule(rule, value, callback)  {
      var nullMsg = "手机号不能为空"
      var errorMsg = '请输入正确的手机号'
      var reg = /^1(3|4|5|7|8)\d{9}$/
      return new Promise((resolve,reject)=>{
        if(value==''){
          reject(nullMsg)
              callback(new Error(nullMsg))
         }else if(!(reg.test(value))){
          reject(errorMsg)
              callback(new Error(errorMsg))
        }else{
          resolve(true)
        }
      })
  };
  
  export function checkEmail(value,nullMsg="邮箱不能为空",errorMsg='请输入正确的邮箱'){
    var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    return new Promise((resolve,reject)=>{
      if(isEmpty(value) && !isEmpty(nullMsg)){
        reject(nullMsg)
       }else if(!isEmpty(value) && !(reg.test(value))){
        reject(errorMsg)
      }else{
        resolve(true)
      }
    })
  }
  export function checkEmailRule(rule, value, callback)  {
      var nullMsg = "邮箱不能为空"
      var errorMsg = '请输入正确的邮箱'
      var reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
      return new Promise((resolve,reject)=>{
        if(value==''){
          reject(nullMsg)
              callback(new Error(nullMsg))
         }else if(!(reg.test(value))){
          reject(errorMsg)
              callback(new Error(errorMsg))
        }else{
          resolve(true)
        }
      })
  };
  
  export function checkUrl(value,nullMsg="网址不能为空",errorMsg = '请输入正确的网址'){
      var reg =  /((http[s]?|ftp):\/\/)?[^\/\.]+?\..+\w$/i
      return new Promise((resolve,reject)=>{
        if(isEmpty(value) && !isEmpty(nullMsg)){
          reject(nullMsg)
         }else if(!isEmpty(value) && !(reg.test(value))){
          reject(errorMsg)
        }else{
          resolve(true)
        }
      })
  }
  
  export function checkUrlRule(rule,value,callback){
      var nullMsg = "网址不能为空"
      var errorMsg = '请输入正确的网址'
      var reg =  /((http[s]?|ftp):\/\/)?[^\/\.]+?\..+\w$/i
      return new Promise((resolve,reject)=>{
        if(value==''){
          reject(nullMsg)
              callback(new Error(nullMsg))
         }else if(!(reg.test(value))){
          reject(errorMsg)
              callback(new Error(errorMsg))
        }else{
          resolve(true)
        }
      })
  }
  
  export function jsonString(value) {
      if (typeof value == 'string') {
          try {
              var obj = JSON.parse(value);
              if (typeof obj == 'object' && obj) {
                  return true;
              } else {
                  return false;
              }
          } catch (e) {
              return false;
          }
      }
      return false;
  }
  
  /**
   * 判断是否为空
   */
  export function isEmpty(value) {
      switch (typeof value) {
          case 'undefined':
              return true;
          case 'string':
              if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
              break;
          case 'boolean':
              if (!value) return true;
              break;
          case 'number':
              if (0 === value || isNaN(value)) return true;
              break;
          case 'object':
              if (null === value || value.length === 0 || Object.keys(value).length == 0) return true;
              var   empty  = true
              for (var i in value) {
                  empty = isEmpty(value[i]);
                  if(!empty){
                      break
                  }
              }
              return empty;
      }
      return false;
  }
  
  /**
   * 是否数组
   */
  export function isArray(value) {
      if (typeof Array.isArray === "function") {
          return Array.isArray(value);
      } else {
          return Object.prototype.toString.call(value) === "[object Array]";
      }
  }
  
  /**
   * 是否对象
   */
  export function isObject(value) {
      return Object.prototype.toString.call(value) === '[object Object]';
  }
  
  // 转移
  export function htmlEncodeByRegExp(str){
      var temp = "";
      if(str.length == 0) return "";
      temp = str.replace(/&/g,"&amp;");
      temp = temp.replace(/</g,"&lt;");
      temp = temp.replace(/>/g,"&gt;");
      temp = temp.replace(/\s/g,"&nbsp;");
      temp = temp.replace(/\'/g,"&#39;");
      temp = temp.replace(/\"/g,"&quot;");
      return temp;
  }
                       /*用正则表达式实现html解码（反转义）*/
  export function htmlDecodeByRegExp(str){
      var temp = "";
      if(str.length == 0) return "";
      temp = str.replace(/&amp;/g,"&");
      temp = temp.replace(/&lt;/g,"<");
      temp = temp.replace(/&gt;/g,">");
      temp = temp.replace(/&nbsp;/g," ");
      temp = temp.replace(/&#39;/g,"\'");
      temp = temp.replace(/&quot;/g,"\"");
      return temp;  
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  